/**
 * 
 */
package com.hjx.service.system.impl;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.flex.remoting.RemotingDestination;
import org.springframework.security.authentication.encoding.Md5PasswordEncoder;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.hjx.dao.RoleMapper;
import com.hjx.dao.UserMapper;
import com.hjx.dao.UserRoleMapper;
import com.hjx.model.Page;
import com.hjx.model.Role;
import com.hjx.model.RoleCriteria;
import com.hjx.model.User;
import com.hjx.model.UserCriteria;
import com.hjx.model.UserRole;
import com.hjx.model.UserRoleCriteria;
import com.hjx.model.UserRoleCriteria.Criteria;
import com.hjx.service.impl.AbstractGenericService;
import com.hjx.service.system.IUserService;

/**
 * @author huangjixin
 * 
 */

@Service(value = "userService")
@RemotingDestination(value = "userService",channels= {"my-amf","my-secure-amf"})
@Transactional
public class UserServiceImpl extends AbstractGenericService<User, Integer> implements
		IUserService {

	@Autowired
	private UserMapper userDao;
	
	@Autowired
	private UserRoleMapper userRoleDao;
	
	@Autowired
	private RoleMapper roleDao;

	/*
	 * 测试字符串。
	 * 
	 * @see com.hjx.service.system.IUserService#getStr()
	 */
//	@Override
//	public String getStr() {
//		return "从UserService接口返回的字符串";
//	}

	@Override
	public List<User> selectAll() {
		List<GrantedAuthority> authsList ;
		
		List<User> list = userDao.selectByExample(new UserCriteria());
		return list;
	}

	@Override
	public Page selectByPage(Page page) {
		UserCriteria userCriteria = new UserCriteria();
		userCriteria.setStart(page.getStart());
		userCriteria.setLimit(page.getLimit());
		int count = userDao.countByExample(new UserCriteria());
		List<User> list = userDao.selectByExample(userCriteria);
		page.setCount(count);
		page.setList(list);
		
		return page;
	}
	
	@Override
	public int deleteByPrimaryKey(Integer id) {
		//-- 先删除子表数据
		UserRoleCriteria userRoleCriteria = new UserRoleCriteria();
		userRoleCriteria.createCriteria().andUserIdEqualTo(id);
		List<UserRole> list = userRoleDao.selectByExample(userRoleCriteria);
		for (UserRole userRole : list) {
			userRoleDao.deleteByPrimaryKey(userRole.getId());
		}
		
		int i = userDao.deleteByPrimaryKey(id);
		return i;
	}

	@Override
	public int insert(User record) {
		super.insert(record);
		Md5PasswordEncoder encoder = new Md5PasswordEncoder();
		String password = encoder.encodePassword(record.getPassword(), record.getUsername());
		record.setPassword(password);
		int i = userDao.insert(record);
		return i;
	}

	@Override
	public int insertSelective(User record) {
		super.insertSelective(record);
		int i = userDao.insertSelective(record);
		return i;
	}

	@Override
	public User selectByPrimaryKey(Integer id) {
		User user = userDao.selectByPrimaryKey(id);
		return user;
	}

	@Override
	public int updateByPrimaryKeySelective(User record) {
		int i = userDao.updateByPrimaryKeySelective(record);
		return i;
	}

	@Override
	public int updateByPrimaryKey(User record) {
		int i = userDao.updateByPrimaryKey(record);
		return i;
	}

	@Override
	public List<Role> getByUserId(Integer userId) {
		UserRoleCriteria userRoleCriteria = new UserRoleCriteria();
		userRoleCriteria.createCriteria().andUserIdEqualTo(userId);
		List<UserRole> userRoleList = userRoleDao.selectByExample(userRoleCriteria);
		
		if(userRoleList == null || userRoleList.size()==0){
			return null;
		}
		RoleCriteria roleCriteria = new RoleCriteria();
		List<Integer> values = new ArrayList<Integer>();
		
		for (UserRole userRole : userRoleList) {
			values.add(userRole.getRoleId());
		}
		
		roleCriteria.createCriteria().andIdIn(values);
		List<Role> list = roleDao.selectByExample(roleCriteria);
		return list;
	}

	/* 关联角色。
	 * @see com.hjx.service.system.IUserService#connectRole(java.lang.Integer)
	 */
	@Override
	public int connectRole(Integer userId, Integer roleId) throws Exception {
		User user = userDao.selectByPrimaryKey(userId);
		Role role = roleDao.selectByPrimaryKey(roleId);
		if(user == null || role == null){
			throw new Exception("请检查数据当中没有id值所对应的记录");
		}
		
		UserRoleCriteria userRoleCriteria = new UserRoleCriteria();
		Criteria criteria = userRoleCriteria.createCriteria();
		criteria.andUserIdEqualTo(userId);
		criteria.andRoleIdEqualTo(roleId);
		
		List<UserRole>list = userRoleDao.selectByExample(userRoleCriteria);
		if(list != null && list.size()>0){
			throw new Exception("已经存在关联关系");
		}
		UserRole record = new UserRole();
		record.setRoleId(roleId);
		record.setUserId(userId);
		
		return userRoleDao.insertSelective(record);
	}

	/* 解除关联角色。
	 * @see com.hjx.service.system.IUserService#connectRole(java.lang.Integer)
	 */
	@Override
	public int disConnectRole(Integer userId, Integer roleId) throws Exception {
		UserRoleCriteria userRoleCriteria = new UserRoleCriteria();
		Criteria criteria = userRoleCriteria.createCriteria();
		criteria.andUserIdEqualTo(userId);
		criteria.andRoleIdEqualTo(roleId);
		
		List<UserRole>list = userRoleDao.selectByExample(userRoleCriteria);
		if(list == null || list.size()==0){
			throw new Exception("不存在关联关系");
		}
		
		return userRoleDao.deleteByPrimaryKey(list.get(0).getId());
	}
}
